var cap4Bridge = (function () {
    /**
     * 创建Cap4桥接器
     */
    var Cap4Bridge = function () {
    }
    /**
     * 关联包路径
     */
    Cap4Bridge.prototype.URLS = {
        collaboration: 'http://collaboration.v5.cmp', // 协同应用
        cap4: 'http://cap4.v5.cmp', // 表单
        cap4report: 'http://cap4report.v5.cmp', // cap4统计，统计详情，历史列表
        cap4home: 'http://cap4home.v5.cmp', // cap4门户首页
        cap4query: 'http://cap4query.v5.cmp', // cap4查询
        cap4unflow: 'http://cap4unflow.v5.cmp'// cap4无流程列表
    };
    /**
     * 取得基础路径
     * 如果是模板：如查询，统计，无流程。
     * 自定义模板情况下跳转至${appId}.v5.cmp
     * params:
     *     route: // 子路径
     *     option:{
     *         relativePath:true // 是否是相对路径
     *         mode: // cap4,cap4report,cap4home,cap4query,cap4unflow
     *         isUseCustomTemplate:true // 是否使用了自定义模板
     *         appId: ,//isUseCustomTemplate为true，则必须传入appId
     *     }
     */
    Cap4Bridge.prototype.createPath = function ( route ,option) {

        // 修改base
        var BASE_URL = '';
        if (option.isUseCustomTemplate) {
            BASE_URL = `${this.URLS[option.mode]}/v1.0.0/htmls/` + route;
            if (cmp.platform.CMPShell) {
                BASE_URL = `http://${appId}.v5.cmp/v1.0.0/dist/index.html`;
            }
        } else {
            BASE_URL = '/seeyon/m3/apps/v5/cap4/htmls/' + route;
        }
        console.log('BASE_URL:',BASE_URL);
        return BASE_URL;
    };
    Cap4Bridge.prototype.openWebView = function (route, data, option) {
        // 默认值
        if (!option) option = {};
        // if (!option.isUseCustomTemplate) option.isUseCustomTemplate = false;
        // if (!option.appId) option.appId = '0';
        // if (!option.mode) option.mode = 'cap4';
        // 获取path
        var url = this.createPath(route,option);
        // 跳转配置，如果是原生才能打开多webview；
        var hrefOptions = {};
        if (cmp.platform.CMPShell) {
            hrefOptions.openWebViewCatch = 1;
        }
        // 跳转
        cmp.href.next(url, data, hrefOptions);
    };
    // 协同应用跳转
    Cap4Bridge.prototype.collaboration = function (type, params) {
        if (type === 'new') {
            collApi.newColl({
                templateId: params.appId,
                newWebView: true
            });
        } else {
            collApi.openList({
                templeteIds: params[0],
                listTitle: params[1],
                openFrom: params[2],
                newWebView: true
            });
        }
    };
    Cap4Bridge.prototype.closeWebView = function (params) {
        cmp.href.back();
    };
    Cap4Bridge.prototype.bundleBackBtn = function (callback) {
        cmp.backbutton();
        cmp.backbutton.push(callback);
    };

    Cap4Bridge.prototype.openLoading = function (params) {
        cmp.dialog.loading(params);
    };
    Cap4Bridge.prototype.closeLoading = function () {
        cmp.dialog.loading(false);
    };

    Cap4Bridge.prototype.cmpAlert = function (msg, callback) {
        cmp.notification.alert(msg, callback, "提示", "确定");
    };

    Cap4Bridge.prototype.toast = function (text, position, time, type) {
        cmp.notification.toast(text, position, time, type);
    };
    Cap4Bridge.prototype.fireEvent = function (params) {
        cmp.webViewListener.fire(params);
    };
    Cap4Bridge.prototype.fireEventByStatistic = function (name, data, handle) {
        if (cmp.platform.CMPShell) {
            cmp.webViewListener.fire({
                type: name,  //此参数必须和webview1注册的事件名相同
                data: data,
                success: function () {
                    handle()
                },
                error: function (error) {
                    handle()
                }
            });
        } else {
            if (data) {
                localStorage.setItem(name, JSON.stringify(data))
                handle()
            }
        }
    };
    Cap4Bridge.prototype.addListenerEventByStatistic = function (name, handle) {
        if (cmp.platform.CMPShell) {
            cmp.webViewListener.addEvent(name, handle);
        }
    };
    Cap4Bridge.prototype.errorHandler = function (error) {
        if (!cmp.errorHandler(error)) { //先调用平台的错误统一处理机制
            //如果平台的统一处理机制处理不了，则自己写业务逻辑
            var code = error.code;
            if (code == 500) {
                params.error(error);
            }
        }
    };
    Cap4Bridge.prototype.ajax = function (params) {
        cmp.ajax({
            type: params.type,
            data: JSON.stringify(params.data),
            url: params.url,
            timeout: params.timeout || '30000',
            dataType: 'json',
            cmpReady2Fire: true,
            fastAjax: true,
            headers: {
                'Content-Type': 'application/json; charset=utf-8',
                'Accept-Language': 'zh-CN',
                'token': cmp.token,
                'option.n_a_s': '1'
            },
            success: params.success,
            error: this.errorHandler
        });
    };
    Cap4Bridge.prototype.platform = function () {
        return cmp.platform
    };
    Cap4Bridge.prototype.serverIp = function () {
        return cmp.serverIp
    };
    Cap4Bridge.prototype.seeyonbasepath = function () {
        return cmp.seeyonbasepath
    };
    Cap4Bridge.prototype.getParams = function () {
        return cmp.href.getParam()
    };

    return new Cap4Bridge();
})();
